Page({
  data: {
    dingdan: []
  },
  s: '',
  onLoad: function(options) {
    this.getData();
  },
  getData: function() {
    const id = wx.getStorageSync('userId');
    const status = wx.getStorageSync('status');
    if (id == 0) {
      wx.showToast({
        title: '请登录才能查看订单',
        icon: 'none'
      });
    } else {
      wx.request({
        url: `http://localhost:8080/order/selectByUserId?userId=${id}&status=${status}`,
        success: (res) => {
          const data = res.data.data;
          console.log(data + "订单数据");
          if (data.length > 0) {
            if (status == 0) {
              this.setData({
                dingdan: data,
                s: "未使用"
              });
            } else {
              this.setData({
                dingdan: data,
                s: "已使用"
              });
            }
          } else {
            wx.showToast({
              title: '还没有订单',
              icon: 'none'
            });
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '数据请求失败',
            icon: 'none'
          });
        }
      });
    }
  },
  handleDingDanTap: function(e) {
    const index = e.currentTarget.dataset.index;
    const orderId = this.data.dingdan[index].id;
    console.log(orderId + "订单id");
    wx.request({
      url: `http://localhost:8080/order/updateStatus?orderId=${orderId}`,
      method: 'PUT',
      success: (res) => {
        this.getData();
      },
      fail: (res) => {
        wx.showToast({
          title: '使用失败',
          icon: 'none'
        });
      }
    });
  }
});